// pages/category/category.js
var networkTool = require("../../utils/networkTool.js");
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: true,
    currentTab: 0,
    productCategory: [],
    brandCategory: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (this.data.currentTab == 0) {
      if (app.globalData.productCategory.length == 0) {
        networkTool.acategory(this.productCategoryData);
      } else {
        this.setProductCategoryData();
      }
    } else if (this.data.currentTab == 1) {
      if (app.globalData.brandCategory.length == 0) {
        networkTool.scategory(this.brandCategoryData);
      } else {
        this.setBrandCategoryData();
      }
    }
  },

  productCategoryData: function (resp) {
    app.globalData.productCategory = resp.VIPCONTENT;
    this.setData({
      productCategory: app.globalData.productCategory
    });
  },

  setProductCategoryData: function (event) {
    var product = app.globalData.productCategory;
    this.setData({
      productCategory: product
    }); 
  },

  brandCategoryData: function (resp) {
    app.globalData.brandCategory = resp.VIPCONTENT;
    this.setData({
      brandCategory: app.globalData.brandCategory
    }); 
  },

  setBrandCategoryData: function (event) {
    var brand = app.globalData.brandCategory;
    this.setData({
      brandCategory: brand
    }); 
  },

  swichNav: function (event) {
    if (this.data.currentTab === event.target.dataset.current) {
      return false;
    } else {
      var showMode = event.target.dataset.current == 0;
      if (showMode) {
        if (app.globalData.productCategory.length == 0) {
          networkTool.acategory(this.productCategoryData);
        } else {
          this.setProductCategoryData();
        }
      }
      else {
        if (app.globalData.brandCategory.length == 0) {
          networkTool.scategory(this.brandCategoryData);
        } else {
          this.setBrandCategoryData();
        }
      }
      this.setData({
        currentTab: event.target.dataset.current,
        isShow: showMode
      })
    }
  },

  onproTab:function(event){ 
    var id = event.currentTarget.dataset.id; 
    app.globalData.searchInfo.ST_FILTERFIELD = "st_web_catalog_"+id;
    wx.navigateTo({
      url: '/pages/item/item'
    })
  },

  onbrdTab: function (event) {
    var brand = event.currentTarget.dataset.brd;
    var id = event.currentTarget.dataset.id;
    app.globalData.searchInfo.ST_FILTERFIELD = "st_web_catalog_" + brand+";st_web_subcatalog_" + id; 
    wx.navigateTo({
      url: '/pages/item/item'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //console.log("onPullDownRefresh");
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //console.log("onReachBottom");
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})